
package com.yuhanginfo.modules.sys.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.service.CrudService;
import com.yuhanginfo.modules.sys.entity.Orders;
import com.yuhanginfo.modules.sys.entity.OrdersDetail;
import com.yuhanginfo.modules.sys.entity.User;
import com.yuhanginfo.modules.sys.utils.CodeUtil;
import com.yuhanginfo.modules.sys.customexception.CustomException;
import com.yuhanginfo.modules.sys.dao.OrdersDao;
import com.yuhanginfo.modules.sys.dao.OrdersDetailDao;

/**
 * 订单变更Service
 * @author yinhao
 * @version 2017-10-13
 */
@Service
@Transactional(readOnly = true)
public class OrdersChangeService extends CrudService<OrdersDao, Orders> {
    @Autowired
    private OrdersDao ordersDao;
    
    
    @Autowired
    private OrdersDetailDao ordersDetailDao;
    

	
	public Map<String,Object> findDetailList(Orders orders) throws CustomException {
		Map<String,Object> resultMap = new HashMap<>();
		Orders ordersResult = ordersDao.getByCode(orders);
		if(!"1".equals(ordersResult.getState())){
			throw new CustomException();
		}
		resultMap.put("orders", ordersResult);
		OrdersDetail queryOrdersDetail = new OrdersDetail();
		queryOrdersDetail.setOrdersCode(orders.getCode());
		resultMap.put("detailList", ordersDetailDao.findAllList(queryOrdersDetail));
		return resultMap;
	}
	
	public Page<Orders> findPage(Page<Orders> page, Orders orders) {
		orders.setPage(page);
		page.setList(ordersDao.findOrdersChangeList(orders));
		return page;
	}
	
	@Transactional(readOnly = false,propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void save(User user,String ordersId,String remarks,String[] id,String[] oldPrice ,String[] newPrice ,String[] oldNum,String[] newNum) {
		boolean alertFlag = false; 
		float totalPrice = 0;
		Date nowDate = new Date();
		List<OrdersDetail> alertOrdersDetail = new ArrayList<>();
		Orders queryOrders = new Orders();
		queryOrders.setId(ordersId);
		Orders tempOrders = ordersDao.get(queryOrders);
		Orders insertOrders = tempOrders.clone();
		String changeCode = CodeUtil.getOrderChangeCode(tempOrders.getCode());
		//筛选修改项
		for(int i=0;i<id.length;i++){
			//计算总价
			totalPrice += Float.parseFloat(newPrice[i]) * Float.parseFloat(newNum[i]);
			//比较数据有没有被修改过
			if(!oldPrice[i].equals(newPrice[i]) || !oldNum[i].equals(newNum[i])){
				alertFlag = true;
				OrdersDetail queryOrdersDetail = new OrdersDetail();
				queryOrdersDetail.setId(id[i]);
				OrdersDetail tempOrdersDetail = ordersDetailDao.get(queryOrdersDetail);
				OrdersDetail insertOrdersDetail = tempOrdersDetail.clone();
				//旧数据变为历史记录
				tempOrdersDetail.setIsHistroy("1");
				tempOrdersDetail.setUpdateDate(nowDate);
				tempOrdersDetail.setModifyPrice(newPrice[i]);
				tempOrdersDetail.setModifyNum(newNum[i]);
				tempOrdersDetail.setOrdersChangeCode(changeCode);
				ordersDetailDao.update(tempOrdersDetail);
				//新增新数据
				insertOrdersDetail.setIsHistroy("0");
				insertOrdersDetail.setNum(newNum[i]);
				insertOrdersDetail.setPrice(newPrice[i]);
				insertOrdersDetail.setUpdateBy(user);
				insertOrdersDetail.setUpdateDate(nowDate);
				//
				alertOrdersDetail.add(insertOrdersDetail);
			}
		}
		//只在有改动的时候去修改主单
		if(alertFlag){
			
			tempOrders.setIsHistory("1");
			tempOrders.setModifyMoney(String.valueOf(totalPrice));
			tempOrders.setModifyFreight(String.valueOf(totalPrice*0.1));
			tempOrders.setUpdateDate(nowDate);
			tempOrders.setChangeCode(changeCode);
			tempOrders.setRemarks(remarks);
			ordersDao.update(tempOrders);
			//
			insertOrders.setMoney(String.valueOf(totalPrice));
			insertOrders.setFreight(String.valueOf(totalPrice*0.1));
			insertOrders.setUpdateBy(user);
			insertOrders.setUpdateDate(nowDate);
			insertOrders.setRemarks(null);
			ordersDao.insert(insertOrders);
			ordersDetailDao.batchInsert(alertOrdersDetail);
		}
		
		
	}

}